/*
  This file is part of SPARK: SharePoint Application Resource Kit.
  The project is distributed via CodePlex: http://www.codeplex.com/spark/
  Copyright (C) 2003-2009 by Thomas Carpe. http://www.ThomasCarpe.com/

  SPARK is free software: you can redistribute it and/or modify
  it under the terms of the GNU General Public License as published by
  the Free Software Foundation, either version 3 of the License, or
  (at your option) any later version.

  SPARK is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with SPARK.  If not, see <http://www.gnu.org/licenses/>.
*/

namespace Behemoth.SharePoint {

    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Web;
    using System.Web.UI;

    using Microsoft.SharePoint;

  /// <summary>
  /// 
  /// </summary>
  /// <remarks>
  /// This class is part of SPARK: SharePoint Application Resource Kit.
  /// Copyright (C) 2007, 2008 by Thomas Carpe. http://www.ThomasCarpe.com/
  /// </remarks>
  public class SPPresenceTools {

    // note that this is also included dynamically from the header pattern in CAML
    public static void AddFixPresenceScript() {
      if (HttpContext.Current == null)
        return;
      Page page = HttpContext.Current.Handler as Page;
      if (page == null)
        return;
      AddFixPresenceScript(page);
    }
    public static void AddFixPresenceScript(Page page) {
      page.ClientScript.RegisterClientScriptInclude("FixPresence", "/_layouts/FixPresence.js");
    }

    private static long _previousTicks = 0;
    private static string GetRandomId() {
      long ticks = DateTime.Now.Ticks;
      if (ticks == _previousTicks)
        _previousTicks = ++ticks;
      return ticks.ToString();
    }

    public static string GetUserWithPresence(bool showPresenceAfterName) {
      SPUser user = SPContext.Current.Web.CurrentUser;
      return GetUserWithPresence(user, showPresenceAfterName);
    }
    public static string GetUserWithPresence(SPUser user, bool showPresenceAfterName) {
      //string userPresence = "<span><img border=\"0\" valign=\"middle\" height=\"12\" width=\"12\" src=\"/_layouts/images/blank.gif\" onload=\"IMNRC('" + user.Email + "')\" id=\"IMID" + user.ID + "\" ShowOfflinePawn=1 /><a href=\"/_layouts/userdisp.aspx?ID=" + user.ID + "\">" + user.Name + "</a></span> (" + user.LoginName + ")";
      //string userPresence = "<SPAN><img border=\"0\" width=\"12\" height=\"12\" src=\"/_layouts/images/imnhdr.gif\" alt=\"\" onload=\"QueuePopulateIMNRC('" + user.Email + "', this)\" name=\"imnmark\" ShowOfflinePawn=\"1\" id=\"user_im,type=sip\" /></SPAN>";
      //userPresence += "<a href=\"/_layouts/userdisp.aspx?ID=" + user.ID + "\">" + user.Name + "</a> (" + user.LoginName + ")";
      string userName = string.Format("{0} ({1})", user.Name, user.LoginName);
      string randomId = GetRandomId();
      string encodedUserEmail = user.Email.Replace("@", "#");
      string nameLink = "<A ONCLICK=\"GoToLink(this);return false;\" HREF=\"/_layouts/userdisp.aspx?ID=" + user.ID + "\">" + user.Name + "</a>";
      string spacer = "<img border=\"0\" height=\"1\" width=\"3\" src=\"/_layouts/images/blank.gif\" alt=\"\" />";
      string iconLink = "<a href=\"javascript:\" onclick=\"IMNImageOnClick();return false;\" class=\"ms-imnlink\">";
      iconLink += "<img onload=\"IMNRC('" + user.Email + "')\" title=\"" + encodedUserEmail + "\" alt=\"No presence information\" name=\"imnmark\" border=\"0\" valign=\"middle\" height=\"12\" width=\"12\" src=\"/_layouts/images/imnhdr.gif\" sip=\"" + user.Email + "\" id=\"imn" + randomId + ",type=sip\" >";
      iconLink += "</a>";
      string format = (showPresenceAfterName) ? "{2}{1}{0}" : "{0}{1}{2}";
      format = "<nobr><span>" + format + "</span></nobr>";
      string userPresence = string.Format(format, iconLink, spacer, nameLink);
      return userPresence;
    }
    public static string GetUserWithPresence(SPFieldUserValue user, bool showPresenceAfterName) {
      return GetUserWithPresence(user.User, showPresenceAfterName);
    }
  
  }

}
